///////////////////////////////////////
//Data set for measures of labor input
///////////////////////////////////////

clear all
use "[FOLDER]\Panel_Ind.dta" 

merge m:1 LopNr_PersonNr using "[FOLDER]\TimeInv_Ind.dta", keepusing(FodelseLandnamn InvAr)
drop if _merge==2
drop _merge


merge m:1 LopNr_PeOrgNr  year using "[FOLDER]\Panel_Firm.dta", keepusing(Ftg_Foradlingsvarde Ftg_Eget_kapital Ftg_Anlaggningstillgangar Org_Sni2007G Ftg_Summa_bruttoinv )

drop if _merge==2
drop _merge
sort LopNr_PeOrgNr year
by LopNr_PeOrgNr year: egen FirmSize=count(LopNr_PeOrgNr)



////Individual income given in 100' kr; div by 10 ==> 1000'
replace KU1Ink = KU1Ink/10
drop if missing(KU1Ink)
tab year, gen(y_)
gen Age = year - Fodelsear


//Generate Education variable with aim to resemble isced11 categories.
nsplit Sun2000niva, digits(2 1) generate(Educ_d12 Educ_d3)

gen isced11 =.
replace isced11  = 1 if Educ_d12==10 | Educ_d12==99
replace isced11  = 2 if Educ_d12==20 | Educ_d12 == 31
replace isced11  = 3 if Educ_d12==32 | Educ_d12 == 33
replace isced11  = 4 if Educ_d12==41
replace isced11  = 5 if Educ_d12==52

replace isced11  = 6 if Educ_d12==53
replace isced11  = 5 if Educ_d12==53 & Educ_d3 == 5
replace isced11  = 7 if Educ_d12==54 | Educ_d12 == 55
replace isced11  = 8 if Educ_d12==60 | Educ_d12 == 62 | Educ_d12==64
replace isced11  = 0 if Educ_d12==00 
tabulate isced11, gen(isced11_dummy)

// Gen. Years of Education 
nsplit Sun2000niva, digits(1 1 1) generate(SunD1 SunD2 SunD3)
gen Educ_Ar = 0
replace Educ_Ar = 3 if SunD1==9
replace Educ_Ar = 6 if SunD1==1
replace Educ_Ar = 9 if SunD1==2
replace Educ_Ar = 10 if SunD1==3 & SunD2==1
replace Educ_Ar = 11 if SunD1==3 & SunD2==2

replace Educ_Ar = 12 if SunD1==3 & SunD2==3
replace Educ_Ar = 13 if SunD1==4 & SunD2==1
replace Educ_Ar = 14 if SunD1==5 & SunD2==2
replace Educ_Ar = 15 if SunD1==5 & SunD2==3
replace Educ_Ar = 16 if SunD1==5 & SunD2==4
replace Educ_Ar = 17 if SunD1==5 & SunD2==5
replace Educ_Ar = 18 if SunD1==6 & SunD2==0
replace Educ_Ar = 18 if SunD1==6 & SunD2==2
replace Educ_Ar = 20 if SunD1==6 & SunD2==4

drop SunD1 SunD2 SunD3


gen Exp=Age - Educ_Ar
gen Exp_Sq=Exp^2
gen Exp_Qu=Exp^3

gen FirmSize1=0
replace FirmSize1=1 if FirmSize<16 & !missing(FirmSize) 
gen FirmSize2=0
replace FirmSize2=1 if FirmSize>15 & FirmSize<51 & !missing(FirmSize)
gen FirmSize3=0
replace FirmSize3=1 if FirmSize>50 & FirmSize<151 & !missing(FirmSize)
gen FirmSize4=0
replace FirmSize4=1 if FirmSize>150  & FirmSize<501 & !missing(FirmSize)
gen FirmSize5=0
replace FirmSize5=1 if FirmSize>500  & !missing(FirmSize)
gen FirmSizeInd=.
replace FirmSizeInd=1 if FirmSize>6 & !missing(FirmSize) 




recode Kon (1=0) //Male
recode Kon (2=1)
gen ChildMig = 0
replace ChildMig = 1 if InvAr - Fodelsear < 17 & FodelseLandnamn!=107
gen SweTime=year-InvAr
replace SweTime=0 if FodelseLandnamn==107 | missing(SweTime)
replace SweTime=0 if SweTime<0
gen Foreign =0
replace Foreign=1 if FodelseLandnamn!=107 


gen logCap=log(Ftg_Anlaggningstillgangar)
gen logFtg_Foradlingsvarde = log(Ftg_Foradlingsvarde)
gen logGrosInvTot=log(Ftg_Summa_bruttoinv)



egen u = rowmiss(y_* FirmSize* Org_Sni2007G logFtg_Foradlingsvarde logCap logGrosInvTot)

gen FR_Incl = 0
replace FR_Incl  = 1 if u==0 & FirmSize>5 

merge 1:1 LopNr_PersonNr LopNr_PeOrgNr year using "[FOLDER]\AppAddLonStat.dta", keepusing(tjomf tjomf_tot arbtim arbtim_tot)

drop if _merge==2
drop _merge 


//Create average "hours worked" variable by country
sort FodelseLandnamn year Kon FR_Incl
by FodelseLandnamn year Kon FR_Incl: egen tjomf_y_av=mean(tjomf) 
sum tjomf_y_av if FodelseLandnamn==107 & FR_Incl==1 & Kon==0
scalar tjomf_107=r(mean)
replace tjomf_y_av=tjomf_y_av/tjomf_107


sum tjomf_y_av if FodelseLandnamn!=107 & FR_Incl==1 & Kon==0
scalar tjomf_av=r(mean)
replace tjomf_y_av=tjomf_av if missing(tjomf_y_av)


//Get predicted units:
gen logKU1 = log(KU1Ink)

reg  logKU1 Exp Exp_Sq Exp_Qu isced11_dummy*  y_* if FodelseLandnamn==107  & FR_Incl==1  & Kon!=1
predict wage1, xb
sum wage1
gen EffUnits=exp(wage1 - r(mean))




save "[FOLDER]\Firm_Labor_OccLayer_Intermediate.dta"


tostring KU1Ssyk3, gen(OccStr3)
gen Occ3_1d=substr(OccStr3,1,1)

destring  Occ3_1d, replace


gen OccGroup=0
replace OccGroup=1 if Occ3_1d==1
replace OccGroup=2 if Occ3_1d==2 | Occ3_1d==3
replace OccGroup=3 if Occ3_1d>3 & Occ3_1d<9 
replace OccGroup=4 if Occ3_1d==9


keep LopNr_PeOrgNr year FR_Incl u FirmSize  KU1Ink  EffUnits  tjomf_y_av FodelseLandnamn Kon OccGroup


replace EffUnits=EffUnits*tjomf_y_av


collapse (sum) KU1Ink  EffUnits (count) count_KU1=KU1Ink (max) FirmSize=FirmSize u=u, by(LopNr_PeOrgNr year FodelseLandnamn Kon OccGroup)

sort LopNr_PeOrgNr year FodelseLandnamn Kon

//Run because I need one variable per group, not one observation per group.

forvalues j=0/4 {
forvalues i=1/129 {
	
	gen EU_M`j'_`i' = 0 
	replace EU_M`j'_`i' = EffUnits if FodelseLandnamn==`i' & OccGroup==`j' & Kon==0
	gen EU_F`j'_`i' = 0 
	replace EU_F`j'_`i' = EffUnits if FodelseLandnamn==`i' & OccGroup==`j' & Kon==1
	

	gen count_M`j'_`i' = 0 
	replace count_M`j'_`i' = count_KU1 if FodelseLandnamn==`i' & OccGroup==`j' & Kon==0
	gen count_F`j'_`i' = 0 
	replace count_F`j'_`i' = count_KU1 if FodelseLandnamn==`i' & OccGroup==`j' & Kon==1

	
}
}



drop count_KU1
collapse (max)  count*  EU_* FirmSize u, by(LopNr_PeOrgNr year)


egen totcount=rowtotal(count_M* count_F*)
egen M_Count=rowtotal(count_M*)
egen F_Count=rowtotal(count_F*)

merge 1:1 LopNr_PeOrgNr year using  "[Folder]\Panel_Firm.dta"

drop if _merge!=3 //Drop obs. w/o a firm				
drop _merge

save "[Folder]\Panel_Firm_Lab_OccLayer.dta" , replace


//Create needed vars:
gen logFtg_Foradlingsvarde=log(Ftg_Foradlingsvarde)


rename Ftg_Summa_bruttoinv GrosInvTot
gen logGrosInvTot = log(GrosInvTot)
gen logGrosInvTot_Sq = logGrosInvTot^2
gen logGrosInvTot_Qu = logGrosInvTot^3
  

gen Cap =  Ftg_Anlaggningstillgangar
gen logCap=log(Cap)
gen logCap_Sq=logCap^2
gen logCap_Qu=logCap^3

bysort Org_BelKommun: egen k_Size=sum(totcount)
sort k_Size
egen k_id =group(k_Size)
sum k_id
scalar maxid=r(max)

//City fixed effects; number 42 chosen to include FE for all counties with >50k inhabitants (the smallest urban area with a FE will have around 27k inhabitants). 
tab Org_BelKommun if k_id>maxid-42 ,gen(k_)
recode k_* (.=0)

drop if FirmSize<6				
gen FirmSize_1=0
replace FirmSize_1=1 if FirmSize<16 & !missing(FirmSize) 
gen FirmSize_2=0
replace FirmSize_2=1 if FirmSize>15 & FirmSize<51 & !missing(FirmSize)
gen FirmSize_3=0
replace FirmSize_3=1 if FirmSize>50 & FirmSize<151 & !missing(FirmSize)
gen FirmSize_4=0
replace FirmSize_4=1 if FirmSize>150  & FirmSize<501 & !missing(FirmSize)
gen FirmSize_5=0
replace FirmSize_5=1 if FirmSize>500  & !missing(FirmSize)

tab year, gen(y_)

gen Ind = substr(Org_Sni2007G,2,.)
destring Ind, replace
tab Ind if u==0, gen(Ind)

sort LopNr_PeOrgNr year

rename EU_F0_107 EU_FNat0
rename EU_F1_107 EU_FNat1
rename EU_F2_107 EU_FNat2
rename EU_F3_107 EU_FNat3 
rename EU_F4_107 EU_FNat4

egen EU_FFor0 = rowtotal(EU_F0_*)
egen EU_FFor1 = rowtotal(EU_F1_*)
egen EU_FFor2 = rowtotal(EU_F2_*)
egen EU_FFor3 = rowtotal(EU_F3_*)
egen EU_FFor4 = rowtotal(EU_F4_*)

drop EU_F0_*
drop EU_F1_*
drop EU_F2_*
drop EU_F3_*
drop EU_F4_*

gen lab_FNat0 = EU_FNat0
gen lab_FFor0 = EU_FFor0
gen lab_FNat1 = EU_FNat1
gen lab_FFor1 = EU_FFor1
gen lab_FNat2 = EU_FNat2
gen lab_FFor2 = EU_FFor2
gen lab_FNat3 = EU_FNat3
gen lab_FFor3 = EU_FFor3
gen lab_FNat4 = EU_FNat4
gen lab_FFor4 = EU_FFor4

	
	
	forvalues i=1(1)129 {
		gen lab_M0_`i' = EU_M0_`i'
		gen lab_M1_`i' = EU_M1_`i'
		gen lab_M2_`i' = EU_M2_`i'
		gen lab_M3_`i' = EU_M3_`i'		
		gen lab_M4_`i' = EU_M4_`i'		

	}

		
//Set natives to reference group, and move the #1 to #107	
rename lab_M0_107 RefGr0
rename lab_M1_107 RefGr1
rename lab_M2_107 RefGr2
rename lab_M3_107 RefGr3
rename lab_M4_107 RefGr4

rename lab_M0_1 lab_M0_107 
rename lab_M1_1 lab_M1_107 
rename lab_M2_1 lab_M2_107 
rename lab_M3_1 lab_M3_107 
rename lab_M4_1 lab_M4_107 




save "[Folder]\Panel_Firm_Lab_OccLayer_temp.dta" , replace


